package com.hyc.gchatsystem.mapper;

import com.hyc.gchatsystem.medel.GroupModel;
import com.hyc.gchatsystem.medel.UserModel;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.*;

/**
 * @projectName: gchatsystem
 * @package: com.hyc.gchatsystem.mapper
 * @className: LocalData
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 * @date: 2022/4/21 17:32
 * @version: 1.0
 */
//存放本地数据
public class LocalData {
    //存储连接的通道 分发消息使用
    public static final List<UserModel> userlist = new ArrayList<>();
    //存储通道和用户id的映射关系 用来获取消息通知的通道
    public static final Map<String, Channel> channelUserUrl = new HashMap<>();
    //在线用户列表
    public static final List<GroupModel> grouplist = new ArrayList<>();
    //群组列表
    public static final ChannelGroup channellist = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    //拿到存储连接通道列表中的通道
    public static List<Channel> getAllChannels() {
        List<Channel> channels = new ArrayList<>();
        Iterator<Channel> iterator = channellist.iterator();
        while (iterator.hasNext()) {
            Channel channel = iterator.next();
            channels.add(channel);
        }
        return channels;
    }

    //通过群组id 取出对应的群组
    public static GroupModel getGroupById(String id) {
        for (GroupModel groupModel : grouplist) {
            if (groupModel.getId().equals(id)) {
                return groupModel;
            }
        }
        return null;
    }
}
